<!DOCTYPE html>
<html lang="">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <title><%= htmlWebpackPlugin.options.title %></title>
    <style>
      /* 隐藏webpack错误弹窗 */
      .webpack-dev-server-client-overlay {
        display: none !important;
      }
    </style>
    <script>
      // 拦截ResizeObserver错误
      const originalError = window.console.error;
      window.console.error = function() {
        if (arguments[0] && typeof arguments[0] === 'string' && 
            (arguments[0].includes('ResizeObserver') || 
             arguments[0].includes('loop completed with undelivered notifications'))) {
          return;
        }
        originalError.apply(console, arguments);
      };

      // 阻止未捕获错误弹窗
      window.addEventListener('error', function(event) {
        if (event.message && 
            (event.message.includes('ResizeObserver') || 
             event.message.includes('loop completed with undelivered notifications'))) {
          event.stopImmediatePropagation();
          event.preventDefault();
          return true;
        }
        
        // 处理资源加载错误
        if (event.target && event.target.src || event.target.href) {
          console.log('资源加载失败:', event.target.src || event.target.href);
          event.preventDefault();
          return true;
        }
      }, true);
      
      // 添加全局处理Chunk加载错误的函数
      function handleChunkError() {
        // 当出现资源加载错误时，清除相关缓存
        if ('caches' in window) {
          caches.keys().then(function(cacheNames) {
            return Promise.all(
              cacheNames.map(function(cacheName) {
                return caches.delete(cacheName);
              })
            );
          });
        }
        
        // 清除localStorage中可能过期的缓存键
        localStorage.removeItem('vueChunkCache');
        
        // 设置标记防止无限刷新
        const refreshCount = parseInt(sessionStorage.getItem('refreshCount') || '0');
        if (refreshCount < 2) {  // 最多刷新两次
          sessionStorage.setItem('refreshCount', (refreshCount + 1).toString());
          setTimeout(() => window.location.reload(), 500);
        }
      }
      
      // 监听未处理的Promise错误
      window.addEventListener('unhandledrejection', function(event) {
        if (event.reason && event.reason.message && 
            event.reason.message.includes('ChunkLoadError')) {
          console.error('检测到Chunk加载错误:', event.reason);
          handleChunkError();
          event.preventDefault();
        }
      });
    </script>
  </head>
  <body>
    <noscript>
      <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>
